﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WMS_BLL;
using WMS_DATABASE_MYSQL.MappingClass;
using WMS_DATABASE_MYSQL.MappingClass.CategoryClass;
using WMS_IBLL;
using WMS_WEBAPI.Controllers.Base;

namespace WMS_WEBAPI.Controllers.Table
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class OrganizationController : ControllerBase
    {
        private readonly ILogger<OrganizationController> _logger;
        private readonly string controllerName;
        private readonly  IOrganizaitonIBLL _organizaitonIBLL;

        public OrganizationController(ILogger<OrganizationController> logger,IOrganizaitonIBLL organizaitonIBLL)
        {
            _logger = logger;
            controllerName = GetType().Name;
            _organizaitonIBLL=organizaitonIBLL;
        }
        /// <summary>
        /// 带有查询功能的分页功能
        /// </summary>
        /// <param name="current"></param>
        /// <param name="pageSize"></param>
        /// <param name="dept"></param>
        /// <param name="section"></param>
        /// <param name="group"></param>
        /// <param name="deptdesc"></param>
        /// <param name="secdesc"></param>
        /// <param name="groupdesc"></param>
        /// <returns></returns>
        //[Authorize(Roles = "superadmin,admin")]
        [HttpGet]
        public async Task<IActionResult> GetOrganizationAsync(int current, int pageSize, int? dept, int? section, int? group, string? deptdesc, string? secdesc, string? groupdesc)
        {
            var data = await _organizaitonIBLL.ToWherePageListAsync(current, pageSize, a => (dept == null || a.dept == dept) && (section == null || a.section == section)
            && (group == null || a.group == group) && (deptdesc == null || a.deptdesc == deptdesc) && (secdesc == null || a.secdesc == secdesc) && (groupdesc == null || a.groupdesc== groupdesc));
            var datacount = data.Count();
            return Ok(new { data, total = datacount,suceess=true });
        }
        /// <summary>
        /// 获取部门信息，用于前端选择框里的内容生成
        /// </summary>
        /// <param name="categoryclass"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetOrganizationDeptAsync()
        {
            var data = await _organizaitonIBLL.QueryListAsync(a=>true);
            var result=data.Select(a => new Deptmodel { dept=a.dept, deptdesc=a.deptdesc}).Distinct().ToList();
            return Ok(result);
        }
        /// <summary>
        /// 获取指定部门信息后的科室信息
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetOrganizationSectionAsync(int dept)
        {
            var data = await _organizaitonIBLL.QueryListAsync(a => a.dept==dept);
            var result = data.Select(a => new Secitonmodel { section = a.section, secdesc = a.secdesc }).Distinct().ToList();
            return Ok(result);
        }
        [HttpGet]
        public async Task<IActionResult> GetOrganizationGroupAsync(int dept,int section)
        {
            var data = await _organizaitonIBLL.QueryListAsync(a => a.dept == dept &&a.section== section);
            var result = data.Select(a => new Groupmodel { group = a.group, groupdesc = a.groupdesc }).Distinct().ToList();
            return Ok(result);
        }
        [HttpDelete]
        public async Task<IActionResult> DelOrganizationAsync([FromBody] List<int> key)
        {
            var t=await _organizaitonIBLL.DeleteRangeAsync(a=>key.Contains(a.key));
            if (t > 0) {
                return Ok("成功删除");
            }
            return BadRequest();
        }

        [HttpPut]
        public async Task<IActionResult> EditOrganizationAsync([FromBody] wms_deptsecgrp_table data)
        {
            var t = await _organizaitonIBLL.EditAsync(data);
            if (t)
            {
                return Ok("成功修改");
            }
            return BadRequest();
        }
        [HttpPost]
        public async Task<IActionResult> AddOrganizationAsync([FromBody] wms_deptsecgrp_table data)
        {
            var t = await _organizaitonIBLL.CreateAsync(data);
            if (t)
            {
                return Ok("成功添加");
            }
            return BadRequest();
        }
        /// <summary>
        /// 获取一个用户的组织机构，发给个人中心上显示
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="section"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserOrganizationAsync(int dept, int section,int group)
        {
            var data = await _organizaitonIBLL.QueryListAsync(a => a.dept==dept && a.section==section && a.group==group);
            var result = data.First();
            return Ok(result);
        }



    }
    /// <summary>
    /// 临时映射类
    /// </summary>
    public class Deptmodel
    {
        public byte dept { get; set; } = 0;
        public string? deptdesc { get; set; } = "";
    }
    public class Secitonmodel
    {
        public byte section { get; set; } = 0;
        public string? secdesc { get; set; } = "";
    }
    public class Groupmodel
    {
        public byte group { get; set; } = 0;
        public string? groupdesc { get; set; } = "";
    }
}
